package net.chathu.airgw.model 
{
	import flash.display.DisplayObject;
	import flash.net.NetGroup;
	import flash.utils.Dictionary;
	import net.chathu.airgw.*;
	/**
	 * ...
	 * @author ze
	 */
	public class SessionData 
	{
		public var SID:String;
		public var soul:Soul;
		public var chatData:ChatData;
		public var nick:String = "";
		
		
		
		public var nickReceived:Boolean = false;
		public var userReceived:Boolean = false;
		public var alternativeLogin:Boolean = false;
		
		public var p2pUserList:UserList;
		private var groupInfos:Dictionary;
		//public var joinedP2PChannels:Dictionary;
		
		public function SessionData(soul:Soul) 
		{
			this.soul = soul;
			this.p2pUserList = new UserList();
			//joinedP2PChannels = new Dictionary(); //channelName
			groupInfos = new Dictionary();
			
		}
		
		public function isInGroup(groupName:String):Boolean {
			//var group:NetGroup = groups[groupName];
			if (groupInfos[groupName])
			{
				return true;
			} else {
				return false;
			}
		}
		
		public function getGroupName(group:NetGroup):String {
			for (var key:String in groupInfos)
			{
				if (P2PGroupInfo(groupInfos[key]).netGroup == group)
				{
					return key;
				}
			}
			return null;
		}
		
		public function getP2PGroupInfo(groupName:String):P2PGroupInfo {
			return P2PGroupInfo(groupInfos[groupName]);
		}	
		
		public function addP2PGroupInfo(groupName:String, groupInfo:P2PGroupInfo):void {
			groupInfos[groupName] = groupInfo;
		}
		
		public function getP2PGroupInfosByUserId(userId:String):Array {
			var ret:Array = new Array();
			
			for each(var groupInfo:P2PGroupInfo in groupInfos)
			{
				if (groupInfo.hasUser(userId))
				{
					ret.push(groupInfo);
				}
			}
			return ret;
		}
		
		public function removeP2PGroupInfo(groupName:String):void {
			delete groupInfos[groupName];
			removeUneccessaryUserData();
		}
		
		private function removeUneccessaryUserData():void {
			var keptUsers:Dictionary  = new Dictionary();
			trace("Users before cleanup: " + p2pUserList.getUserNum());
			for each(var groupInfo:P2PGroupInfo in groupInfos) {
				var users:Array = groupInfo.getUsers();
				for (var i:int = 0; i < users.length; i++) {
					
					keptUsers[users[i]] = true; 
					//Jelenetes ~:--> keptUsers["42223-63623-727234272-131254124"] = true;
				}
			}
			
			var p2pusers:Dictionary = p2pUserList.getDictionary();
			for (var userId:String in p2pusers)
			{
				if (!keptUsers[userId])
				{
					var p2pUser:P2PUser = P2PUser(p2pUserList.getUser(userId));
					p2pUserList.remove(p2pUser);
					trace("DELETE USER FROM P2P USERLIST: " +  p2pUser.name);
				}
			}
			trace("Users after cleanup: " + p2pUserList.getUserNum());
		}
		
		public function getJoinedGroupInfos():Dictionary {
			/*var ret:Array = new Array();
			for (var name:String in groupInfos)
			{
				ret.push(ret);
			}
			return ret;*/
			return groupInfos;
		}
		
		
		public function destroy():void {
			this.SID = null;
			this.soul = null;
			this.chatData = null;
			this.nick = null;
			if (this.p2pUserList)
			{
			this.p2pUserList.clear();
			}
			//this.p2pUserList = null;
		}
		
		public function get yourHost():String {
			return nick + "!~you@localhost";
		}
		
	}

}